var wxpay = require('../../utils/pay.js')
var app = getApp()
Page({
  data: {
    statusType: ["我是团长", "我是团员", ],
    currentType: 0,
    tabClass: ["", "",],
    domain: app.globalData.domain,
    //shop_domin: app.globalData.domain.replace('/api/v1','')
  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.onShow();
  },
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id
    var bulkId = e.currentTarget.dataset.bulkid;
    var orderIndex = e.currentTarget.dataset.index;
    wx.showModal({
      title: '确定要退出该拼团吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          if (that.data.currentType == 0){
            wx.request({
              url: app.globalData.domain + '/wxapp/bulk_purchase/' + bulkId + '/',
              header: {
                'Authorization': 'JWT ' + wx.getStorageSync('token')
              },
              method: 'PUT',
              data: {
                group_active: 3,
                item: that.data.bulklist[orderIndex].item,
                order: that.data.bulklist[orderIndex].order,
                bulk_purchase_tpl: that.data.bulklist[orderIndex].bulk_purchase_tpl,
              },
              success: (res) => {
                wx.hideLoading();
                that.onShow();
                if (res.statusCode == 200) {}
              }
            })
          }else{
            wx.request({
              url: app.globalData.domain + '/wxapp/order/' + orderId + '/',
              header: {
                'Authorization': 'JWT ' + wx.getStorageSync('token')
              },
              method: 'PUT',
              data: {
                order_status: 0,
                wxapp_suborder_set: that.data.bulklist[orderIndex].order_detail.wxapp_suborder_set
              },
              success: (res) => {
                wx.hideLoading();
                that.onShow();
                if (res.statusCode == 200) { }
              }
            })

          }
          
        }
      }
    })
  },
  toPayTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;
    var needScore = e.currentTarget.dataset.score;
    var orderIndex = e.currentTarget.dataset.index;
    wx.request({
      url: app.globalData.domain + '/wxapp/bulk_purchase/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token')
      },
      data:{
        order: orderId,
      },
      success: (res) => {
        wx.request({
          url: app.globalData.domain + '/wxapp/payment/' + orderId + '/',
          header: {
            'Authorization': 'JWT ' + wx.getStorageSync('token')
          },
          success: (res) => {
            console.log(res, 'payment')
            wx.hideLoading();
            if (res.statusCode == 200) {
              //let is_commander = res.data.is_commander
              wx.requestPayment(
                {
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  'success': function (res) {
                    console.log(res, '66')
                    wx.request({
                      url: app.globalData.domain + '/wxapp/order/' + orderId + '/',
                      header: {
                        'Authorization': 'JWT ' + wx.getStorageSync('token')
                      },
                      method: 'PUT',
                      data: {
                        order_status: 11,
                        wxapp_suborder_set: that.data.bulklist[orderIndex].order_detail.wxapp_suborder_set
                      },
                      success: (res) => {
                        //console.log(res.data)
                        wx.hideLoading();
                      }
                    })
                  },
                  'fail': function (res) {
                    wx.showModal({
                      title: '错误',
                      content: res.err_desc,
                      showCancel: false
                    })
                  },
                  'complete': function (res) {
                    that.onShow();
                  }
                })
            }
          }
        })

      }
    })
    
    
       
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载

  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },

  onShow: function () {
    // 获取订单列表
    wx.showLoading();
    var that = this;
    var postData = {
      token: wx.getStorageSync('token')
    };
    postData.status = that.data.currentType;
    //console.log(that.data.currentType, "that.data.currentType")
      wx.request({
        url: app.globalData.domain + '/wxapp/bulk_purchase/',
        header: {
          'Authorization': 'JWT ' + wx.getStorageSync('token')
        },
        data: {
          isself: 1,
          user:1,
          group_active:3,
          is_commander: that.data.currentType == 0?'True':'False'
        },
        success: (res) => {
          //console.log(res.data, "onshow_/wxapp/bulk_purchase/")
          wx.hideLoading();
          if (res.statusCode == 200) {
            that.setData({
              bulklist: res.data,
            });
          } else {
            this.setData({
              orderList: null,
              logisticsMap: {},
              goodsMap: {}
            });
          }
        }
      })
    
    

  },

  // pintuan:function(){

  // },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  }
})
